package uestc.lj.basic.sort;

/**
 * 在有序数组中，是否存在一个数
 *
 * @Author:Crazlee
 * @Date:2021/11/20
 */
public class Code07_BinaryExist {
    public static void binaryExist(int[] array, int number) {
        if (array == null || array.length == 0) {
            return;
        }
        int left = 0, right = array.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (array[mid] == number) {
                System.out.println("array数组中存在数" + number + "，该数在数组中的下标为：" + mid);
                break;
            } else if (array[mid] < number) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        if (left >= right) {
            System.out.println("array数组中不存在" + number);
        }
    }

    public static void main(String[] args) {
        int[] array = {1, 3, 5, 7, 9, 11, 13, 25, 67};
        binaryExist(array, 7);
    }
}
